package com.tc.xushu;

import com.tc.config.SpringDataJPAConfig;
import com.tc.repositories.CustomerRepository;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;

import javax.persistence.EntityManager;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Proxy;

/**
 * @Auther: tianchao
 * @Date: 2021/12/5 20:46
 * @Description:
 */
public class MainStart {
    public static void main(String[] args) {
        AnnotationConfigApplicationContext ioc = new AnnotationConfigApplicationContext(SpringDataJPAConfig.class);
        //CustomerRepository bean = ioc.getBean(CustomerRepository.class);
        //System.out.println(bean.findById(10L));
        // entityManage
        LocalContainerEntityManagerFactoryBean localContainerEntityManagerFactoryBean = ioc.getBean(LocalContainerEntityManagerFactoryBean.class);
        EntityManager em = localContainerEntityManagerFactoryBean.createNativeEntityManager(null);
        //clazz
        ParameterizedType genericInterface = (ParameterizedType)CustomerRepository.class.getGenericInterfaces()[0];
        Class actualTypeArgument = (Class)genericInterface.getActualTypeArguments()[0];

        CustomerRepository o = (CustomerRepository)Proxy.newProxyInstance(CustomerRepository.class.getClassLoader(),
                new Class[]{CustomerRepository.class},
                new MyJpaRepository(em,actualTypeArgument));
        System.out.println(o.findById(1L));
    }
}
